const app = getApp()
Component({
  options: {
    addGlobalClass: true,
  },
  properties: {
    userNotification: {
      type: String,
      value: ''
    }
  },
  data: {
    showNotification: true,
    likeCount: 0,
    starCount: 0,
    voiceCount: 0,
    waitCount: 0,
    applyCount: 0,
    userInfo: {},
    colorList: ['blue', 'green', 'orange', 'red', 'mauve'],

    avatarCache: '',
    bgCache: ''
  },
  attached() {
    let that = this;
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    })
    let i = 0;
    if (Object.keys(app.sumCount).length == 0){
      wx.cloud.callFunction({
        name: 'getVoiceNumber'
      }).then(res => {
        let countRes = res.result
        app.sumCount = {
          voiceCount: countRes.voiceCount,
          likeCount: countRes.likeCount,
          starCount: countRes.starCount,
          waitCount: countRes.waitCount,
          applyCount: countRes.applyCount
        }
        this.setData({
          waitCount: countRes.waitCount,
          applyCount: countRes.applyCount
        })
        numDH(countRes.voiceCount, countRes.likeCount, countRes.starCount)
      })
    }else{
      numDH(app.sumCount.voiceCount, app.sumCount.likeCount, app.sumCount.starCount)
      this.setData({
        waitCount: app.sumCount.waitCount,
        applyCount: app.sumCount.applyCount
      })
    }
    function numDH(voice, like, star) {
      if (i < 20) {
        setTimeout(() => {
          that.setData({
            likeCount: i,
            starCount: i,
            voiceCount: i
          })
          i++
          numDH(voice, like, star);
        }, 20)
      } else {
        that.setData({
          likeCount: that.coutNum(like),
          starCount: that.coutNum(star),
          voiceCount: that.coutNum(voice)
        })
      }
    }
    this.setData({
      userInfo: app.user
    })
    wx.getStorage({
      key: 'avatar-' + app.userDataId,
      success : (res) => {
        this.setData({
          avatarCache: res.data
        })
      }
    })
    wx.getStorage({
      key: 'bg-' + app.userDataId,
      success: (res) => {
        this.setData({
         bgCache: res.data
        })
      }
    })
    wx.hideLoading()
  },
  methods: {
    hideNotification() {
      this.setData({
        showNotification: false
      })
    },
    lookHome() {
      wx.navigateTo({
        url: '/pages/personalHome/index?openid=' + app.user._openid,
      })
    },
    coutNum(e) {
      if (e > 1000 && e < 10000) {
        e = (e / 1000).toFixed(1) + 'k'
      }
      if (e > 10000) {
        e = (e / 10000).toFixed(1) + 'W'
      }
      return e
    },
    // CopyLink(e) {
    //   wx.setClipboardData({
    //     data: e.currentTarget.dataset.link,
    //     success: res => {
    //       wx.showToast({
    //         title: '已复制',
    //         duration: 1000,
    //       })
    //     }
    //   })
    // },
    showModal(e) {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    },
    hideModal(e) {
      this.setData({
        modalName: null
      })
    },
    showQrcode() {
      wx.previewImage({
        urls: ['https://636f-conch-voice-test-8c0757-1259463971.tcb.qcloud.la/打赏.jpeg?sign=e8fffff062781a259a0c2e413ca06e7b&t=1560868357'],
        current: 'https://636f-conch-voice-test-8c0757-1259463971.tcb.qcloud.la/打赏.jpeg?sign=e8fffff062781a259a0c2e413ca06e7b&t=1560868357' // 当前显示图片的http链接      
      })
    },
  }
})